package server;
import java.io.IOException;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import net.SocketHandler;

import model.Room;


/*
 * A simple chat server, the structure and programming style of this server is very similar to the 'DragonServer'
 * written by Hu Xiang (mostly) and me during my last summer intern ship, thanks a lot to him.
 * Li Jie, eldron@163.com
 */
public class ServerMain {
	private static ExecutorService pool;
	
	public static void main(String [] args){
		pool = Executors.newCachedThreadPool();
		Room room = new Room();
		System.err.println("=====================>>>>>>>>>>>>> Server started up <<<<<<<<<<<<<<<<<<==============");
		
		try {
			ServerSocket server = new ServerSocket(SystemConstants.PORT);
			
			while(true){
				Socket sock = server.accept();
				System.err.println("=============>>>>>>>>>>>>> Connection from " + sock.getInetAddress().getHostAddress());
				sock.setKeepAlive(true);
				sock.setSoTimeout(0);
				pool.submit(new SocketHandler(sock, room));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
